<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>仓库信息列表</el-breadcrumb-item>
    </el-breadcrumb>

    <div class="autumn-center">
      <!-- 搜索栏 -->
      <div class="query-form">
        <autumn-search :modelValue="queryInfo" @search="search">
          <template #default>
            <el-col :span="4">
              <el-form-item prop="code" label="仓库">
                <el-input v-model="queryInfo.code" placeholder="编码/名称" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item prop="category" label="分类">
                <el-select v-model="queryInfo.category">
                  <el-option v-for="(value, key, index) in warehouseCategorys" :key="index" :label="value" :value="key">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item prop="isEnable" label="启用">
                <el-select v-model="queryInfo.isEnable" placeholder="请选择">
                  <el-option label="是" value="1" />
                  <el-option label="否" value="0" />
                </el-select>
              </el-form-item>
            </el-col>
          </template>
        </autumn-search>
      </div>

      <!-- 数据表格 -->
      <div class="main-body top">
        <autumn-table ref="mainTableRef" :page="page" @changePage="search">
          <template #button>
            <el-button type="primary" icon="el-icon-plus" v-if="$hasPermission('wms:warehouse-info:add')"
              @click="toView">新增
            </el-button>
          </template>
          <template #column>
            <el-table-column type="index" label="#" fixed="left" align="center">
              <template #default="scope">
                {{ initIndex(scope.$index) }}
              </template>
            </el-table-column>
            <el-table-column prop="code" label="仓库编码" width="200" />
            <el-table-column prop="name" label="仓库名称" width="200" />
            <el-table-column prop="category" label="分类" align="center" width="100">
              <template #default="scope">
                {{ warehouseCategorys[scope.row.category] }}
              </template>
            </el-table-column>
            <el-table-column label="启用" align="center" width="100">
              <template #default="scope">
                <el-switch v-model="scope.row.isEnable" :active-value="1" :inactive-value="0"
                  :disabled="!$hasPermission('wms:warehouse-info:enable&wms:warehouse-info:disable')"
                  @change="changeEnabled(scope.row)" />
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" />
            <el-table-column prop="createName" label="创建人" width="100" />
            <el-table-column prop="createTime" label="创建时间" align="center" width="150"
              :formatter="(row: any) => $moment(row.createTime, 'YYYY-MM-DD HH:mm')" />
            <el-table-column label="操作" fixed="right" align="center" width="200">
              <template #default="scope">
                <el-button-group>
                  <el-button type="info" icon="el-icon-view" size="small" title="查看" @click="toView(scope.row, '1')" />
                  <el-button type="primary" icon="el-icon-edit" size="small" title="编辑"
                    v-if="$hasPermission('wms:warehouse-info:update')" @click="toView(scope.row)" />
                  <el-button type="danger" icon="el-icon-delete" size="small" title="删除"
                    v-if="$hasPermission('wms:warehouse-info:delete')" @click="del(scope.row)" />
                </el-button-group>
              </template>
            </el-table-column>
          </template>
        </autumn-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import hooks from 'src/hook/index'
import { $hasPermission, $confirmMsg, $listDict, $moment } from 'src/util/tools'
import { pageWarehouseInfo, enableWarehouseInfo, disableWarehouseInfo, deleteWarehouseInfo } from 'src/api/wms/warehouseInfo'

const mainTableRef = ref()
const router = useRouter()

// 查询仓库分类
const warehouseCategorys = $listDict('WAREHOUSE_CATEGORY')

// 分页查询
const queryInfo = reactive({
  code: '',
  category: '',
  property: '',
  isEnable: ''
})
const { page, initIndex, search } = hooks.useTable((pageNumber: number, pageSize: number) => {
  pageWarehouseInfo({ ...queryInfo, pageNumber, pageSize }).then(({ data }) => {
    page.data = data.list
    page.total = data.total
  })
})

// 启/禁用仓库信息
const changeEnabled = function (row: any) {
  const id = row.id
  const isEnable = row.isEnable
  if (isEnable) {
    enableWarehouseInfo(id).then(() => {
      search()
    })
  } else {
    disableWarehouseInfo(id).then(() => {
      search()
    })
  }
}

// 删除仓库信息
const del = function (row: any) {
  const ids = [row.id]
  $confirmMsg('确认删除吗？', () => {
    deleteWarehouseInfo(ids).then(() => {
      search()
    })
  })
}

// 查看仓库信息
const toView = function (row: any, type?: string) {
  const id = row.id
  router.push({
    path: type ? '/wms/warehouseInfo/warehouseInfoView' : '/wms/warehouseInfo/warehouseInfoForm',
    query: { id }
  })
}

</script>

<style lang="scss" scoped></style>
